﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainAttendanceRecordQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        #endregion

        #region Constructor
        public frmMaintainAttendanceRecordQuery()
        {
            InitializeComponent();
        }

        public frmMaintainAttendanceRecordQuery(MstPersonEntity person, string date)
        {
            InitializeComponent();
            if (person != null)
            {
                txtPerson.Tag = person;
                txtPerson.Text = person.NameCn;
            }
            if (string.IsNullOrEmpty(date) == false && date.Length == 7)
            {
                DateTime startDate = Convert.ToDateTime(date + "-01");
                DateTime endDate = startDate.AddMonths(1).AddDays(-1);
                dtpAttendanceDate.EditValue = startDate;
                dtpAttendanceDateTo.EditValue = endDate;
            }
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND D.DEPARTMENT_ID = '" + department.DepartmentId + "'";
            }
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity person = (MstPersonEntity)txtPerson.Tag;
                condition += " AND P.PERSON_ID = '" + person.PersonId + "'";
            }
            if (dtpAttendanceDate.EditValue!=null)
            {
                condition += " AND AR.ATTENDANCE_DATE >= '" + Convert.ToDateTime(dtpAttendanceDate.EditValue).ToString("yyyy-MM-dd") + "'";
            }
            if (dtpAttendanceDateTo.EditValue != null)
            {
                condition += " AND AR.ATTENDANCE_DATE <= '" + Convert.ToDateTime(dtpAttendanceDateTo.EditValue).ToString("yyyy-MM-dd") + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "P.PERSON_CODE, AR.ATTENDANCE_DATE DESC";
            DataTable dt = HrAttendanceRecordBs.GetDetailByCondition(condition, orderBy);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Tag = frm.SelectedDepartment;
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
            }
            else
            {
                txtDepartment.Tag = null;
                txtDepartment.Text = "";
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn;
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }

        #endregion

    }
}
